﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using OpenComposite.EII.CodeGenerators;
using OpenComposite.EII.Repository;
using System.IO;
using System.Diagnostics;

namespace OpenComposite.EII.Forms
{
	public partial class FormDeployServiceModule : DockContent
	{
		#region Initialization
		public FormDeployServiceModule(ServiceModule svcmodule)
		{
			_svcmodule = svcmodule;

			InitializeComponent();

			txtSvcModuleName.Text = _svcmodule.Name;
			txtTargetPath.Text = _svcmodule.ExtendedProperties.DeployPath;
			switch ( _svcmodule.ExtendedProperties.BuildType ) {
				case WorkflowBuildType.ASMX_IIS:
					//cmbWSType.SelectedIndex = 1;
					_svcmodule.ExtendedProperties.BuildType = WorkflowBuildType.WCF_IIS;
					cmbWSType.SelectedIndex = 0;
					break;
				case WorkflowBuildType.WCF_IIS:
					cmbWSType.SelectedIndex = 0;
					break;
				default:
					cmbWSType.SelectedIndex = 0;
					break;
			}
		}
		#endregion

		#region Protected Members

		#region Methods
		// override
		protected override void OnClosed(EventArgs e)
		{
			_svcmodule.ExtendedProperties.DeployPath = txtTargetPath.Text;
			_svcmodule.Save();

			base.OnClosed(e);
		}
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private ServiceModule _svcmodule;
		#endregion

		#region Methods
		private bool selectTargetPath()
		{
			string path = txtTargetPath.Text;
			if ( !string.IsNullOrEmpty(path) && Directory.Exists(path) ) {
				dlgSelectFolder.SelectedPath = path;
				if ( !dlgSelectFolder.SelectedPath.EndsWith("\\") )
					dlgSelectFolder.SelectedPath += "\\";
			}
			if ( dlgSelectFolder.ShowDialog() == DialogResult.OK ) {
				txtTargetPath.Text = dlgSelectFolder.SelectedPath;
				_svcmodule.ExtendedProperties.DeployPath = txtTargetPath.Text;
				return true;
			} else {
				return false;
			}
		}
		private void deployComposite(bool update)
		{
			try {
				if ( string.IsNullOrEmpty(txtTargetPath.Text) || !Directory.Exists(txtTargetPath.Text) ) {
					if ( !selectTargetPath() ) return;
				}
				CompositeDeployment deployment = new CompositeDeployment(_svcmodule);
				Debugger.Break();
				//deployment.DeployComposite(_svcmodule, txtTargetPath.Text, update);
			} catch ( Exception ex ) {
				MessageBox.Show(ex.Message, btnUpdate.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		#endregion

		#region Event Handlers
		private void btnUpdate_Click(object sender, EventArgs e)
		{
			deployComposite(true);
		}

		private void btnRegenerate_Click(object sender, EventArgs e)
		{
			deployComposite(false);
		}

		private void btnSelectTargetPath_Click(object sender, EventArgs e)
		{
			selectTargetPath();
		}

		private void cmbWSType_SelectedIndexChanged(object sender, EventArgs e)
		{
			switch ( cmbWSType.SelectedIndex ) {
				case 1:
					_svcmodule.ExtendedProperties.BuildType = WorkflowBuildType.ASMX_IIS;
					break;
				default:
					_svcmodule.ExtendedProperties.BuildType = WorkflowBuildType.WCF_IIS;
					break;
			}
		}
		#endregion

		#endregion
	}
}
